// pages/work/list.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 'tab1',
        loading1: false,
        loading2: false,
        loading3: false,
        tip1: '上拉加载',
        tip2: '',
        tip3: '上拉加载',
        p1: 1,
        p2: 1,
        p3: 1,
        is_pull1: true,
        is_pull2: true,
        is_pull3: true,
        myupload: [],

        //第一次进入tab2的标记
        enter1: true,
        enter2: false,
        enter3: false,
        tip: '上拉加载',
        data1: [],
        data2: [],
        data3: [],
        row: 15,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    height: (((res.windowHeight - 42) * 750) / res.windowWidth)
                })
            }
        })
      this.onPullDownRefresh();
    },
  
    handleChange({ detail }) {
        this.setData({
            current: detail.key
        });
        if (this.data.current === 'tab1') {
          
            this.data.data1 = [];
            this.getlist(0);
        } else if (this.data.current === 'tab2') {
            
            this.data.data2 = [];
            this.getlist(1);
        }else if (this.data.current === 'tab3') {
          
            this.data.data3 = [];
            this.getlist(2);
        }
    },
    

    getlist: function (type) {
        this.data.current === 'tab1' && (this.setData({
            loading1: true,
            tip1: '正在加载',
        }));
        this.data.current === 'tab2' && (this.setData({
            loading2: true,
            tip2: '正在加载',
        }));
        this.data.current === 'tab3' && (this.setData({
            loading3: true,
            tip3: '正在加载',
        }));
        let _this = this;
        app.http.$ajax({ url: 'v2.task/mytask',data:{type:type}},{
            success(res) {
                let data = res.data;
                if (_this.data.current === 'tab1') {
                    if (data.data.length < _this.data.row) {
                        _this.data.is_pull1 = false;
                    }
                    _this.data.data1.push(...data.data)
                    _this.setData({
                        data1: _this.data.data1,
                        loading1: false,
                        tip1: data.data.length < _this.data.row ? '无更多数据': '上拉加载',
                    });
                } else if (_this.data.current === 'tab2') {
                    if (data.data.length < _this.data.row) {
                        _this.data.is_pull2 = false;
                    }
                    _this.data.data2.push(...data.data)
                    _this.setData({
                        data2: _this.data.data2,
                        loading2: false,
                        tip2: data.data.length < _this.data.row ? '无更多数据' : '上拉加载',
                    });
                } else if (_this.data.current === 'tab3') {
                    if (data.data.length < _this.data.row) {
                        _this.data.is_pull3 = false;
                    }
                    _this.data.data3.push(...data.data)
                    _this.setData({
                        data3: _this.data.data3,
                        loading3: false,
                        tip3: data.data.length < _this.data.row ? '无更多数据' : '上拉加载',
                    });
                }
            }
        })
    },
    //滚动到底部的方法
    lower: function () {
        //上拉加载之前要判断一下是否可以上拉
        if(this.data.current === 'tab1') {
            if (this.data.is_pull1 === true) {
                this.getlist(0);
            }
        }

        if (this.data.current === 'tab2') {
            if (this.data.is_pull2 === true) {
                this.getlist(1);
            }
        }
        if (this.data.current === 'tab3') {
            if (this.data.is_pull3 === true) {
                this.getlist(2);
            }
        }
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        if (this.data.current === 'tab1') {
            this.data.enter1 = false;
            this.data.data1 = [];
            this.data.p1 = 1;
            this.data.is_pull1 = true;
            if (this.data.enter1 === false) {
                this.data.enter1 = true;
                this.getlist(0);
            }
        } else if (this.data.current === 'tab2') {
            this.data.enter2 = false;
            this.data.data2 = [];
            this.data.p2 = 1;
            this.data.is_pull2 = true;
            if (this.data.enter2 === false) {
                this.data.enter2 = true;
                this.getlist(1);
            }
        } else if (this.data.current === 'tab3') {
            this.data.enter3 = false;
            this.data.data3 = [];
            this.data.p3 = 1;
            this.data.is_pull3 = true;
            if (this.data.enter3 === false) {
                this.data.enter3 = true;
                this.getlist(2);
            }
        }     
    },

})